//
//  LGHomeViewController.m
//  LuxuryGoods
//
//  Created by 纳里健康 on 2018/5/23.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGHomeViewController.h"
#import "LGHomeViewController+subViews.h"
#import "Masonry.h"
#import "MainApi.h"
#import "MessageListViewController.h"
#import "LGHomeSearcjViewController.h"

@interface LGHomeViewController ()<UINavigationControllerDelegate,UIScrollViewDelegate>
{
//    UIScrollView *_backScrollView;
    
    UIView *_searchBackView;
    
    UIView *_styleView;
    
    UIView *_titleView;
    
    UIButton *_msgBtn;
    
    UIButton *_topButton;
    
    dispatch_group_t _group;
    
    CGFloat _collectionMinY;
    
}


@end

@implementation LGHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationController.delegate = self;
    
    self.productDataArray = [NSMutableArray array];
    
    
     _group = dispatch_group_create();
    
    dispatch_group_enter(_group);

    [self homeRequest];
    
    dispatch_group_enter(_group);
    
    [self getProductListData];
    

    dispatch_group_notify(_group, dispatch_get_main_queue(), ^{
        
        NSLog(@"00000");
        [self makeNavBar];
        
        //创建回到顶部的按钮
        self->_topButton = [self makeButtonWithImage:@"btn_top" action:@selector(goToTop)];
        [self .view addSubview:self->_topButton];
        [self->_topButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view).offset(-20);
            make.bottom.offset(-20);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];

        
    });
    
    
    
}

#pragma mark -- 加载首页数据
- (void)homeRequest
{
    [[MainApi sharedInstance] getPath:@"Index/index" params:nil resultBlock:^(NSDictionary *responseObject, NSError *error) {
        NSInteger code = [responseObject[@"code"] integerValue];
        
        if(code == 1)
        {
            self.dataModel = [LGHomeModel yy_modelWithJSON:responseObject[@"data"]];
            
            [self makeScrollViewAndCircleScroll];
            
            [self makeSubViews];
            
            dispatch_group_leave(self->_group);
        }
    
    }];
}

#pragma mark -- 加载猜你喜欢的数据
- (void)getProductListData
{
    [[MainApi sharedInstance] getPath:@"Goods/getList" params:@{@"is_recom":@"0"} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        NSInteger code = [responseObject[@"code"] integerValue];
        
        if(code == 1)
        {
            dispatch_group_leave(self->_group);
            
            NSDictionary *dict = responseObject[@"data"];
            
            NSArray *goodArray = dict[@"goods_list"];
            
            for (NSDictionary *goodDict in goodArray) {
                
                LGGoodsModel *model = [LGGoodsModel yy_modelWithJSON:goodDict];
                
                [self.productDataArray addObject:model];
            }
            
            [self.collectionView reloadData];
        }
        
    }];
}


#pragma mark -- 加载subView
/*
    1.顶部的搜索View
    2.中间的各种View
 */

- (void)makeSubViews
{
   [self makeTopSearchView];
    

    UIView *fiveBtnView = [self makeFiveBtnViewWithFrame:CGRectMake(0, (SCREEN_WIDTH / 72) * 35 + 10, SCREEN_WIDTH, 90)];
    
    [_backScrollView addSubview:fiveBtnView];
    
    UIView *styleView = [self makeStyleViewWithFram:CGRectMake(0, CGRectGetMaxY(fiveBtnView.frame) + 10, SCREEN_WIDTH, 400)];
    [_backScrollView addSubview:styleView];

    UIView *brandView = [self makeBrandViewWithFrame:CGRectMake(0, CGRectGetMaxY(styleView.frame) + 10, SCREEN_WIDTH, 110)];
    
    [_backScrollView addSubview:brandView];
    
    UIView *TwoImageView = [self makeTwoImageViewWithFrame:CGRectMake(0, CGRectGetMaxY(brandView.frame) + 10, SCREEN_WIDTH, 200 * self.dataModel.activity.count + 10 * self.dataModel.activity.count - 1)];
    [_backScrollView addSubview:TwoImageView];
    
    
    UIView *labeView = [self makeLabelViewWithFrame:CGRectMake(0, CGRectGetMaxY(TwoImageView.frame) + 10, SCREEN_WIDTH, 50)];
    [_backScrollView addSubview:labeView];
    
    self.headerHeight = CGRectGetMaxY(labeView.frame) + 10;
    
    
    [self makeRecommendProductViewWithFrame:CGRectMake(0, CGRectGetMaxY(TwoImageView.frame) + 10, SCREEN_WIDTH, SCREEN_HEIGHT)];//474
}

#pragma mark -- 初始化headerView
- (void)makeScrollViewAndCircleScroll
{
    _backScrollView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT)];
    
    _backScrollView.backgroundColor = RGBCOLOR(239, 241, 241);
    
    
    self.automaticallyAdjustsScrollViewInsets = false;
    [self.view addSubview:_backScrollView];
    
    //添加轮播
    [_backScrollView addSubview:[self makeCircleImageView]];

}

#pragma mark -- TopView
- (void)makeTopSearchView
{
    //创建一个带搜索框的navigationBar
    _searchBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NAVIGATOR_HEIGHT)];
    [self.backScrollView addSubview:_searchBackView];
    
    UIButton *searchBtn = [self makeSearBar];
    [_searchBackView addSubview:searchBtn];
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.right.offset(-60);
        make.height.mas_equalTo(30);
        make.centerY.offset(0);
    }];
    
    //先创建一个信息按钮
    _msgBtn = [self makeButtonWithImage:@"btn_msg" action:@selector(goToShowMessage)];
    [_searchBackView addSubview:_msgBtn];
    [_msgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.mas_equalTo(self->_searchBackView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}


#pragma mark --
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.collectionView)
    {
        CGFloat offSetY = scrollView.contentOffset.y;

        if(offSetY >= (SCREEN_WIDTH / 72) * 35)
        {
            _searchBackView.alpha = 0;

            _titleView.alpha = 1;
        }else
        {
            _searchBackView.alpha = 1;

            _titleView.alpha = 0;
        }

        if(offSetY >= SCREEN_HEIGHT)
        {
            _topButton.hidden = NO;
        }else
        {
            _topButton.hidden = YES;
        }
        
    }
}
#pragma mark -- titleView
- (void)makeNavBar
{
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NAVIGATOR_HEIGHT)];
    _titleView.backgroundColor = WHITE_COLOR;
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"奢物";
    titleLabel.textColor = UIColorFromRGB(0xB1A071);
    [_titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_titleView);
        make.centerY.equalTo(self->_titleView.mas_top
                             ).offset(20 + 22 + (IS_IPHONE_X ? 24 : 0));
    }];
    [self.view addSubview:_titleView];
    
//    if(!_msgBtn)
//    {
        _msgBtn = [self makeButtonWithImage:@"btn_msg" action:@selector(goToShowMessage)];
//    }
    [_titleView addSubview:_msgBtn];
    [_msgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.mas_equalTo(titleLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    
    UIButton *severBtn = [self makeButtonWithImage:@"LightSS" action:@selector(callService)];
    [_titleView addSubview:severBtn];
    [severBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self->_msgBtn.mas_left).offset(-10);
        make.centerY.mas_equalTo(titleLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
//    _titleView.backgroundColor = [UIColor cyanColor];
    _titleView.alpha = 0;
    
}

- (UIButton *)makeButtonWithImage:(NSString *)imageStr action:(SEL)btnAction
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    
    [button addTarget:self action:btnAction forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

#pragma mark -- btnAction
- (void)goToShowMessage
{
    MessageListViewController *messageVC = [MessageListViewController new];
    [self.navigationController pushViewController:messageVC animated:YES];
    NSLog(@"查看消息");
}

- (void)callService
{
    LGHomeSearcjViewController *searchVC = [LGHomeSearcjViewController new];
    [self.navigationController pushViewController:searchVC animated:YES];
    NSLog(@"联系客服");
}

#pragma mark -- 回到顶部
- (void)goToTop
{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.collectionView.contentOffset = CGPointMake(0, 0);
        
    }];

}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
